package com.lechi.iot.netty.channel;

import com.lechi.iot.netty.message.TcpMessage;
import com.lechi.iot.netty.service.NameValueImpl;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

import java.net.InetSocketAddress;

public class TcpContextChannel extends NameValueImpl implements ITcpChannel {

    private final ChannelHandlerContext ctx;

    private final String hostName;

    private String deviceSn;

    public TcpContextChannel(ChannelHandlerContext ctx) {
        this.ctx = ctx;
        InetSocketAddress address = (InetSocketAddress) this.ctx.channel().remoteAddress();
        this.hostName = address.getHostString();
    }

    @Override
    public void send(ByteBuf msg) {
        this.ctx.writeAndFlush(msg);
    }

    public void send(TcpMessage msg) {
        this.ctx.writeAndFlush(msg);
    }

    @Override
    public String getDeviceSn() {
        return deviceSn;
    }

    @Override
    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn;
    }

    @Override
    public String hostName() {
        return hostName;
    }

}
